

class LoggingCfgFileGenerator(object):
    RTYPE_DEFAULT = ''
    RTYPE_UT = 'ut'
    RTYPE_DT = 'dt'
    def __init__(self, outputFile, loggerNameList, rtype='',  windows=False):
        self.loggerNameList = loggerNameList
        self.rtype = rtype
        self.windows = windows
        self.outputFile = outputFile

    def generate(self):
        loggerDeclaration = self.__generateLoggerDeclaration()
        handlerDeclaration = self.__generateHandlerDeclaration()
        formatterDeclaration = self.__generateFormatterDeclaration()
        loggers = self.__generateLoggers()
        handlers = self.__generateHandlers()
        formatters = self.__generateFormatters()
        with open(self.outputFile, 'w') as f:
            f.write(loggerDeclaration)
            f.write(handlerDeclaration)
            f.write(formatterDeclaration)
            f.write(loggers)
            f.write(handlers)
            f.write(formatters)


    def __generateLoggerDeclaration(self):
        return  '''
[loggers]
keys=root,%s

''' % ','.join(self.loggerNameList)

    def __generateHandlerDeclaration(self):
        return '''
[handlers]
keys=consolethreadhandler,%s

'''%','.join([logger+'handler' for logger in self.loggerNameList])

    def __generateFormatterDeclaration(self):
        return '''
[formatters]
keys=fileformat, consoleformat, filethreadformat, consolethreadformat

'''

    def __generateLoggers(self):
        loggers = '''
[logger_root]
level = ERROR
handlers=

'''
        for logger in self.loggerNameList:
            loggers += '''
[logger_%s]
level=DEBUG
handlers=%shandler, consolethreadhandler
qualname=%s
propagate=0

'''% (logger,logger, logger)

        return loggers


    def __generateHandlers(self):
        handlers = '''
[handler_consolethreadhandler]
class=StreamHandler
level=DEBUG
formatter=consoleformat
args=(sys.stdout,)

'''
        for logger in self.loggerNameList:
            if self.windows:
                if self.rtype != LoggingCfgFileGenerator.RTYPE_DEFAULT:
                    logPath = 'log\\'+self.rtype+'\\'+logger+'.log'
                else:
                    logPath = 'log\\'+logger+'.log'
            else:
                if self.rtype != LoggingCfgFileGenerator.RTYPE_DEFAULT:
                    logPath = 'log/'+self.rtype+'/'+logger+'.log'
                else:
                    logPath = 'log/'+logger+'.log'

            handlers += '''
[handler_%shandler]
class=handlers.TimedRotatingFileHandler
level=DEBUG
formatter=filethreadformat
args=('%s', 'd', 1)

''' % (logger, logPath)

        return handlers

    def __generateFormatters(self):
        return '''
[formatter_fileformat]
format=[%(levelname)s] [%(module)s] [%(funcName)s] [%(lineno)d] [%(asctime)s]: %(message)s

[formatter_filethreadformat]
format=[%(levelname)s] [%(module)s] [%(thread)d] [%(threadName)s] [%(funcName)s] [%(lineno)d] [%(asctime)s]: %(message)s

[formatter_consoleformat]
format=[%(levelname)s] [%(module)s] [%(funcName)s] [%(lineno)d] [%(asctime)s]: %(message)s

[formatter_consolethreadformat]
format=[%(levelname)s] [%(module)s] [%(thread)d] [%(threadName)s] [%(funcName)s] [%(lineno)d] [%(asctime)s]: %(message)s

'''